//
//  BaseViewController.m
//  Custom_Weibo
//
//  Created by LEO on 12/7/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()

@end

@implementation BaseViewController {
    UIView *_tipView;
    MBProgressHUD *_hud;
}

#pragma mark - SYSTEM_METHODS
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadImage];
    [self setNavigationItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - INITIALIZATION_METHODS
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self getNotification];
    }
    return  self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self getNotification];
    }
    return self;
}

#pragma mark - MONITOR_NOTIFICATION
- (void)getNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(themeDidChange:)
                                                 name:kThemeNotification
                                               object:nil];
}

#pragma mark - WHEN_THEME_CHANGED
- (void)themeDidChange:(NSNotification *)notification {
    [self loadImage];
}

#pragma mark LOAD_IMAGE
- (void)loadImage {
    ThemeManager *manager = [ThemeManager defaultManager];
    UIImage *BGImage = [manager getThemeImage:@"bg_home.jpg"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:BGImage];
}

#pragma mark SET_NAVIGATION_ITEMS
- (void)setNavigationItem {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Setting"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(settingAction:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit"
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(editAction:)];
}

#pragma mark - WHEN_LOADING
- (void)showLoading:(BOOL)show {
    if (_tipView == nil) {
        CGRect tipFrame = CGRectMake(0, 0, kHUDWidth, kHUDHeight);
        _tipView = [[UIView alloc] initWithFrame:tipFrame];
        _tipView.backgroundColor = kRGB(0, 0, 0, 0.4);
        
        //  1)Add loading view
        UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [activityView startAnimating];
        activityView.center = _tipView.center;
        [_tipView addSubview:activityView];
        
        //  2)Add tip label
        UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        tipLabel.backgroundColor = [UIColor clearColor];
        tipLabel.textColor = [UIColor whiteColor];
        tipLabel.text = @"Loading...";
        [tipLabel sizeToFit];
        [_tipView addSubview:tipLabel];
        
        //  Set position
        tipLabel.top = activityView.bottom;
    }
    
    if (show) {
        [self.view addSubview:_tipView];
    }else {
        if (_tipView.superview) {
            [_tipView removeFromSuperview];
        }
    }
}

#pragma mark - HUD
- (void)showHUD:(NSString *)title {
    if (_hud == nil) {
        _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
    [_hud show:YES];
    _hud.labelText = title;
    _hud.dimBackground = YES;
}

- (void)hideHUD {
    [_hud show:NO];
}

- (void)didFinishHUD:(NSString *)title {
    _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark"]];
    _hud.mode = MBProgressHUDModeCustomView;
    _hud.labelText = title;
    
    [_hud hide:YES afterDelay:2];
}

#pragma mark - FLIP_AVAILABLE
- (void)settingAction:(UIBarButtonItem *)button {
    //  Use SDK to make left controller can flip to front by touch the button
    MMDrawerController *mmDrawer = self.mm_drawerController;
    [mmDrawer openDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

- (void)editAction:(UIBarButtonItem *)button {
    MMDrawerController *mmDrawer = self.mm_drawerController;
    [mmDrawer openDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}

#pragma mark - DEALLOC
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:kThemeNotification];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
